Create Project: springboot_junit (add Spring Boot Starters from the table)
Create Package: controllers (inside main package)
– Create Class: MyController.java (inside controllers package)
Create Test Class: MyControllerTest.java
MyController.java
package com.ivoronline.springboot_junit.controllers;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MyController {
@ResponseBody
@RequestMapping("/Hello")
public String hello() {
return "Hello from Controller";
}
}
MyControllerTest.java
package com.ivoronline.springboot_junit.controllers;
import com.ivoronline.springboot_junit.controllers.MyController;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class MyControllerTest {
@Test
void hello() {
//PERFORM ACTION
MyController myController = new MyController();
String result = myController.hello();
//TEST RESULT
assertEquals("Hello from Controller", result);
}
}